﻿using HpxWMS_Api.Entities;
using HpxWMS_Api.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HpxWMS_Api.DAL
{

    /// <summary>
    /// 仓库数据访问层
    /// </summary>
    public class WarehouseServices
    {
        /// <summary>
        /// ef实例
        /// </summary>
        private HpxWMSContext whcontext = new HpxWMSContext();


        /// <summary>
        /// 查询一页记录
        /// </summary>
        /// <param name="wq"></param>
        /// <returns></returns>
        public List<Warehouse> FindPage(Wq wq)
        {
            return whcontext.Warehouses.Where(warehouse => warehouse.Name.Contains(wq.SearchText)
                                          || warehouse.Manager.NickName.Contains(wq.SearchText)
                                          || warehouse.Address.Contains(wq.SearchText)
                                          || wq.SearchText==null
                                          || wq.SearchText=="")
                                         .OrderBy(warehouse => warehouse.Id)
                                         .Skip(wq.PageSize * (wq.PageIndex - 1))
                                         .Take(wq.PageSize)
                                         .ToList();
        }
        /// <summary>
        /// 返回符合要求的总记录数
        /// </summary>
        /// <param name="searchText"></param>
        /// <returns></returns>
        public int Count(string searchText)
        {
            return whcontext.Warehouses.Where(warehouse => warehouse.Name.Contains(searchText)
                                          || warehouse.Address.Contains(searchText)
                                          || searchText == null
                                          || searchText == "")
                                          .Count();
        }

        /// <summary>
        /// 添加仓库
        /// </summary>
        /// <param name="wi"></param>
        /// <returns></returns>
        public bool Insert(Wi wi)
        {
            Warehouse warehouse = new Warehouse();
            warehouse.Name = wi.Name;
            warehouse.Address = wi.Address;
            warehouse.UserId = wi.ManageId;
            warehouse.Mobile = wi.Mobile;
            whcontext.Warehouses.Add(warehouse);
            try
            {
                whcontext.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 更新仓库
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool Updata(Wu wu)
        {
            var editwarehouse = whcontext.Warehouses.Find(wu.Id);
            editwarehouse.Name = wu.Name;
            editwarehouse.Address = wu.Address;
            editwarehouse.UserId = wu.ManageId;
            editwarehouse.Mobile = wu.Mobile;
            try
            {
                whcontext.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 删除仓库
        /// </summary>
        /// <returns></returns>
        public bool Remove(int id)
        {
            var warehouse = whcontext.Warehouses.Find(id);
            whcontext.Warehouses.Remove(warehouse);
            whcontext.SaveChanges();
            try
            {
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 查询仓库名称是否重名
        /// </summary>
        /// <returns></returns>
        public bool ExictBywareName(string wareName)
        {
            return whcontext.Warehouses.Where(Warehouse => Warehouse.Name == wareName).Count() > 0;
        }

        /// <summary>
        /// 判断更新的仓库是否已创建
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool ExictBywareNameandNotEqualId(string name, int id)
        {
            return whcontext.Warehouses.Where(warehouse => warehouse.Name == name && warehouse.Id != id).Count() > 0;
        }
    }
}